package org.liuh.t2020.netty.httpserver01;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @Description TODO 测试Netty作为Http服务类.
 * @Author liuh
 */
public class TestNettyHttpServer {

    public static void main(String[] args) throws Exception {
        // 创建两个线程组... boss负责接收请求，worker负责过滤请求.
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        // 创建通道启动器！ ServerBooStrap 可以方便快速的启动 管道.实现访问
        // sub-class which allows easy bootstrap of
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        try {
            // 设置管道类型, 添加处理器！ .
            serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new TestServerInitializer());
            ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
            // 同步channelFuture关闭.
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }

}
